package com.scott.customer.service.impl;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Consumer;
import com.scott.customer.service.MyConsumer;
import com.scott.customer.service.RabbitService;
import com.scott.customer.util.RabbitUtil;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

@Service
public class RabbitServiceImpl implements RabbitService {
    private String QUERY_Q1 = "query_q1";
    private String EXCHANGE_N1 = "exchange_n1";
    private String QUERY_Q2 = "query_q2";
    private String EXCHANGE_N2 = "exchange_n2";
    private String QUERY_Q3 = "query_q3";
    private String EXCHANGE_N3 = "exchange_n3";

    public void getMsg() throws IOException, TimeoutException {
        Connection connection = RabbitUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.queueDeclare(QUERY_Q1, false, false, false, null);

        channel.queueBind(QUERY_Q1, EXCHANGE_N1, "", null);
        channel.basicQos(1);
        Consumer consumer = new MyConsumer(channel);

        channel.basicConsume(QUERY_Q1, true, consumer);
    }

    public void getMsgDirect() throws IOException, TimeoutException {
        Connection connection = RabbitUtil.getConnection();
        Channel channel = connection.createChannel();
        channel.basicQos(1);
        channel.queueDeclare(QUERY_Q3, false, false, false, null);
        channel.queueBind(QUERY_Q3,EXCHANGE_N3, "scott.*");
        channel.basicConsume(QUERY_Q3, true, new MyConsumer(channel));
    }
}
